﻿namespace WMS6.Web.CrossDock
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.CrossdockServiceRef;
    using WMS6.Web.UserControls;

    public class FlowThruAllocationList : BasePage
    {
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnNewSearch;
        protected Button btnReSearch;
        protected HtmlForm F79622;
        protected GridView gvApportionmentRule;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected Label lblArea;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2ApportionmentRuleList(ApportionRuleQueryEntity apportionRuleQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(apportionRuleQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = CrossdockServiceClientProxy.GetInstance().GetApportionRuleInfoByQueryEntity(apportionRuleQuery);
            base.BindData2GridView(this.gvApportionmentRule, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvApportionmentRule, "OWNER_ID");
                List<string> CustomerID = base.GetSelectedIDsInGridView(this.gvApportionmentRule, "CUSTOMER_ID");
                List<string> SeqNo = base.GetSelectedIDsInGridView(this.gvApportionmentRule, "SEQ_NO");
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C008");
                }
                else
                {
                    this.lblMsg.Text = CrossdockServiceClientProxy.GetInstance().DeleteApportionRuleInfo(selectedID, CustomerID, SeqNo);
                    ApportionRuleQueryEntity apportionRuleQuery = this.GetApportionRuleQueryEntity();
                    this.BindData2ApportionmentRuleList(apportionRuleQuery);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvApportionmentRule, "OWNER_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("008");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("009");
            }
            else
            {
                base.Response.Redirect(string.Format("FlowThruApportionmentRule.aspx?from=list&ID={0}", base.Server.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            ApportionRuleQueryEntity apportionRuleQuery = this.GetApportionRuleQueryEntity();
            apportionRuleQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = apportionRuleQuery.CurrentPage;
            apportionRuleQuery.CurrentPage = "0";
            this.gvApportionmentRule.Columns[1].Visible = false;
            this.gvApportionmentRule.AllowSorting = false;
            this.BindData2ApportionmentRuleList(apportionRuleQuery);
            this.hdnActionID.Value = "";
            this.gvApportionmentRule.ShowFooter = false;
            apportionRuleQuery.IsGetAll = false;
            apportionRuleQuery.CurrentPage = currentPageIndex;
            this.SetApportionRuleQueryEntity(apportionRuleQuery);
            base.Export2Excel("ApportionRule.xls", this.gvApportionmentRule);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            ApportionRuleQueryEntity apportionRuleQuery = this.GetApportionRuleQueryEntity();
            apportionRuleQuery.CurrentPage = "0";
            this.BindData2ApportionmentRuleList(apportionRuleQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            ApportionRuleQueryEntity apportionRuleQuery = this.GetApportionRuleQueryEntity();
            apportionRuleQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2ApportionmentRuleList(apportionRuleQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            ApportionRuleQueryEntity apportionRuleQuery = this.GetApportionRuleQueryEntity();
            apportionRuleQuery.OwnerID = "";
            apportionRuleQuery.CustomerID = "";
            apportionRuleQuery.SeqNo = "";
            apportionRuleQuery.AllowOverShipment = "";
            apportionRuleQuery.MaximumPerOver = "";
            this.BindData2ApportionmentRuleList(apportionRuleQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            ApportionRuleQueryEntity apportionRuleQuery = this.GetApportionRuleQueryEntity();
            if (int.Parse(apportionRuleQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                apportionRuleQuery.CurrentPage = (int.Parse(apportionRuleQuery.CurrentPage) + 1).ToString();
                this.BindData2ApportionmentRuleList(apportionRuleQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            ApportionRuleQueryEntity apportionRuleQuery = this.GetApportionRuleQueryEntity();
            if (int.Parse(apportionRuleQuery.CurrentPage) > 0)
            {
                apportionRuleQuery.CurrentPage = (int.Parse(apportionRuleQuery.CurrentPage) - 1).ToString();
                this.BindData2ApportionmentRuleList(apportionRuleQuery);
            }
        }

        private ApportionRuleQueryEntity GetApportionRuleQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as ApportionRuleQueryEntity);
            }
            return (base.PageQueryEntitySession as ApportionRuleQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                ApportionRuleQueryEntity apportionRuleQuery = this.GetApportionRuleQueryEntity();
                apportionRuleQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2ApportionmentRuleList(apportionRuleQuery);
            }
        }

        protected void gvApportionmentRule_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F79622", this.gvApportionmentRule);
        }

        protected void gvApportionmentRule_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvApportionmentRule_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                ApportionRuleQueryEntity apportionRuleQuery = this.GetApportionRuleQueryEntity();
                apportionRuleQuery.SortField = e.SortExpression;
                if (apportionRuleQuery.SortDirection == "ASC")
                {
                    apportionRuleQuery.SortDirection = "DESC";
                }
                else if (apportionRuleQuery.SortDirection == "DESC")
                {
                    apportionRuleQuery.SortDirection = "ASC";
                }
                this.BindData2ApportionmentRuleList(apportionRuleQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            ApportionRuleQueryEntity apportionRuleQuery = new ApportionRuleQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                apportionRuleQuery.OwnerID = this.PreviousPage.OwnerID;
                apportionRuleQuery.CustomerID = this.PreviousPage.CustomerID;
                apportionRuleQuery.SeqNo = this.PreviousPage.SeqNo;
                apportionRuleQuery.AllowOverShipment = this.PreviousPage.AllowOverShipment;
                apportionRuleQuery.MaximumPerOver = this.PreviousPage.MaximumPerOver;
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            apportionRuleQuery.IsGetAll = false;
            apportionRuleQuery.CurrentPage = "0";
            apportionRuleQuery.PageSize = this.gvApportionmentRule.PageSize.ToString();
            apportionRuleQuery.SortField = "ROWID";
            apportionRuleQuery.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvApportionmentRule.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                this.gvApportionmentRule.Columns[1].Visible = false;
                this.gvApportionmentRule.Columns[2].Visible = true;
                this.btnNew.Visible = false;
                this.btnEdit.Visible = false;
                this.btnDelete.Visible = false;
                this.btnReSearch.Visible = false;
                this.btnExport.Visible = false;
            }
            else if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetApportionRuleQueryEntity(apportionRuleQuery);
            this.BindData2ApportionmentRuleList(apportionRuleQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetApportionRuleQueryEntity(ApportionRuleQueryEntity apportionRuleQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = apportionRuleQuery;
            }
            else
            {
                base.PageQueryEntitySession = apportionRuleQuery;
            }
        }

        public FlowThruAllocationQuery PreviousPage
        {
            get
            {
                return (FlowThruAllocationQuery) base.PreviousPage;
            }
        }
    }
}

